package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.CustomerTransferMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService {
    @Autowired
    private CustomerTransferMapper transferMapper;
    @Autowired
    private CustomerMapper customerMapper;

    public PageInfo<CustomerTransfer> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<CustomerTransfer> list = transferMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public void save(CustomerTransfer entity) {
        //也可以先检查客户的状态,然后再操作
        //========================
        //记录移交信息,自动填充操作人和操作时间
        entity.setOperateTime(new Date());
        entity.setOperator(UserContext.getCurrentUser());
        transferMapper.insert(entity);
        //修改该客户的营销人员
        customerMapper.updateSeller(entity.getNewSeller().getId(), entity.getCustomer().getId());
    }
}
